package com.wiz.web.controllor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.service.SyncService;
import com.wiz.web.vo.criteria.AdminCriteria;
import com.wiz.web.vo.criteria.HqStationCriteria;

@Controller
@RequestMapping("/sync")
public class SyncControllor extends BaseControllor{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private SyncService syncService;

	/**
	 * 初始化站点
	 * @return
	 */
	@RequestMapping(value = "/station", method = RequestMethod.POST)
	@ResponseBody
	public String syncStation(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		try{
			HqStationCriteria criteria = gson.fromJson(params, new TypeToken<HqStationCriteria>(){}.getType());
			return syncService.syncStationDetail(criteria);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("syncStation error in "+stationId+"  params="+params+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 初始化站点
	 * @return
	 */
	@RequestMapping(value = "/permission", method = RequestMethod.POST)
	@ResponseBody
	public String syncAdminClientPermission(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		try{
			AdminCriteria criteria = gson.fromJson(params, new TypeToken<AdminCriteria>(){}.getType());
			return syncService.syncAdminClientPermission(criteria);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("syncAdminClientPermission error in "+stationId+"  params="+params+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 初始化员工卡登录
	 * @return
	 */
	@RequestMapping(value = "/logincard", method = RequestMethod.POST)
	@ResponseBody
	public String syncAdminLoginCard(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		try{
			AdminCriteria criteria = gson.fromJson(params, new TypeToken<AdminCriteria>(){}.getType());
			return syncService.syncAdminLoginInfo(criteria);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("syncAdminLoginCard error in "+stationId+"  params="+params+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 常用配置
	 * @return
	 */
	@RequestMapping(value = "/config", method = RequestMethod.POST)
	@ResponseBody
	public String syncConfig(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		try{
			HqStationCriteria criteria = gson.fromJson(params, new TypeToken<HqStationCriteria>(){}.getType());
			return syncService.syncConfig(criteria);
		}catch (Exception e) {
			e.printStackTrace();
			log.error("syncAdminLoginCard error in "+stationId+"  params="+params+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
}
